package com.sunchangpeng.wechat.mp.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunchangpeng.wechat.core.WechatClient;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import com.sunchangpeng.wechat.mp.user.bean.UserPage;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class BlackLists {
    private WechatClient wechatClient;

    public void setWechatClient(WechatClient wechatClient) {
        this.wechatClient = wechatClient;
    }

    public static BlackLists with(MpAppSetting appSetting) {
        BlackLists blackLists = new BlackLists();
        blackLists.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return blackLists;
    }

    /**
     * 拉黑用户
     */
    public void batchblacklist(List<String> openids) {
        String url = EndpointProvider.get("url.blacklist.black");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject().fluentPut("opened_list", openids)));
    }

    /**
     * 取消拉黑用户
     */
    public void batchunblacklist(List<String> openids) {
        String url = EndpointProvider.get("url.blacklist.unblack");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject().fluentPut("opened_list", openids)));
    }

    /**
     * 获取公众号的黑名单列表
     */
    public UserPage getblacklist() {
        return getblacklist("");
    }

    /**
     * 获取公众号的黑名单列表
     */
    public UserPage getblacklist(String nextOpenId) {
        String url = EndpointProvider.get("url.blacklist.list");
        String response = wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject().fluentPut("begin_openid", nextOpenId)));
        return JSON.parseObject(response, UserPage.class);
    }
}
